﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class AB55AV95Data : BaseFunction
    {
        #region -------------function -------------------------
        public int ThreadCount;
        public AB55AV95Data()
            : base()
        {

        }
        public AB55AV95Data(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public AB55AV95Data(OracleConnection _OracleDB, IntexReptServBLClient _proxy)
            : base(_OracleDB, _proxy)
        {

        }
        ~AB55AV95Data()
        {
            if (OracleDB != null)
            {
                OracleDB.Close();
            }
        }
        public DataSet GetABPOS(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            set4Return.Tables.Add("ABPOS");
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                string ABPAUFKRS = dwABPOS["ABPAUFKRS"].ToString();
                string ABPAUFLFD = dwABPOS["ABPAUFLFD"].ToString();
                string ABPAUFPOS = dwABPOS["ABPAUFPOS"].ToString();
                string ABPART = dwABPOS["ABPART"].ToString();
               // string ABPLIEFNR = dwABPOS["LSNAME1"].ToString().Trim();
                /*
                or1.CommandText = "SELECT DISTINCT ABPAUFKRS,ABPAUFLFD,ABPWVDAT,ABPAUFPOS,ABPART, ABPVAR, ABPEALFM,ABPFARBE,'#' AS GAART,'#' AS GAVAR,'#' AS GAFARBE"
                + ",ABPBESCHICH,ABPRESMGE,ABPSOLLMENGE1,ABPRESSTK,ABPZUGSTK,ABPLIEFDAT,P.ABPERLKZ,P.ABPERLDAT,LSLIEFNA AS WPRODUCER"
                   + ",WARFDDIS AS GWEFTDEN,WAEINZNR AS GREEDNO,'' AS WEFTYARNSTATUS"
                   + ",BZBAUMKRS||BZBAUMLFD AS SBEAMNO,BZSOLLVLG AS SBEAMLENG,BZAENDDAT AS SENDDATE ,BZDATRPM"
                   + ",ABADIAUFKRS AS WBAUFKRS,ABADIAUFLFD AS WBAUFLFD,ABADIAUFPOS AS WBAUFPOS,ABPAUFART"
                   + ",ABFKENNUNG,ABFISTARTDAT,ABFIENDDAT"
                   + " FROM ABPOS P LEFT JOIN WASTAMM ON P.ABPART=WAVWART AND TRIM(WAVWVAR)=TRIM(ABPVAR)"
                   + " LEFT JOIN ABAUFM ON ABAAPOS='0001' AND ABAAUFKRS=ABPAUFKRS AND ABAAUFLFD=ABPAUFLFD AND ABAAUFPOS=ABPAUFPOS "
                   + " LEFT JOIN BAZU ON BZAUFKRS=ABPAUFKRS AND BZAUFLFD=ABPAUFLFD AND BZAUFPOS=ABPAUFPOS"
                   + " LEFT JOIN LIEFDAT L ON  P.ABPLIEFNR=L.LSLIEFNR"
                   + " LEFT JOIN ABFOLGE F ON F.ABFAUFKRS=P.ABPAUFKRS AND F.ABFAUFLFD=P.ABPAUFLFD AND F.ABFAUFPOS=P.ABPAUFPOS AND ABFKENNUNG='RP'"
                   + "  WHERE ABPMANDANT='00'   "
                    + " AND ABPAUFKRS=:ABPAUFKRS"
                    + " AND ABPAUFLFD =:ABPAUFLFD"
                    + " AND ABPAUFPOS =:ABPAUFPOS "
                    + " ORDER BY  1,2,3";*/
                or1.CommandText = "SELECT DISTINCT ABPAUFKRS,ABPAUFLFD,ABPWVDAT,ABPAUFPOS,ABPART, ABPVAR, ABPEALFM,ABPFARBE,'#' AS GAART,'#' AS GAVAR,'#' AS GAFARBE"
              + ",ABPBESCHICH,ABPRESMGE,ABPSOLLMENGE1,ABPRESSTK,ABPZUGSTK,ABPLIEFDAT,P.ABPERLKZ,P.ABPERLDAT,LSNAME1 AS WPRODUCER"
                 + ",WARFDDIS AS GWEFTDEN,WAEINZNR AS GREEDNO,'' AS WEFTYARNSTATUS"
                 + ",ABADIAUFKRS AS WBAUFKRS,ABADIAUFLFD AS WBAUFLFD,ABADIAUFPOS AS WBAUFPOS,ABPAUFART"
                 + ",ABFKENNUNG,ABFISTARTDAT,ABFIENDDAT"
                 + " FROM ABPOS P LEFT JOIN WASTAMM ON P.ABPART=WAVWART AND TRIM(WAVWVAR)=TRIM(ABPVAR)"
                 + " LEFT JOIN ABAUFM ON ABAAPOS='0001' AND ABAAUFKRS=ABPAUFKRS AND ABAAUFLFD=ABPAUFLFD AND ABAAUFPOS=ABPAUFPOS "
                 + " LEFT JOIN LIEFDAT L ON  P.ABPLIEFNR=L.LSLIEFNR"
                 + " LEFT JOIN ABFOLGE F ON F.ABFAUFKRS=P.ABPAUFKRS AND F.ABFAUFLFD=P.ABPAUFLFD AND F.ABFAUFPOS=P.ABPAUFPOS AND ABFKENNUNG='RP'"
                 + "  WHERE ABPMANDANT='00' AND ABPERLKZ=:ABPERLKZ  "
                  + " AND ABPAUFKRS=:ABPAUFKRS"
                  + " AND ABPAUFLFD =:ABPAUFLFD"
                  + " AND ABPAUFPOS =:ABPAUFPOS AND ABPART LIKE '%" + ABPART + "%' "
                  //+ " AND ABPLIEFNR LIKE '%" + ABPLIEFNR + "%' "
                  + " ORDER BY  1,2,3";
                or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("ABPAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                or1.Parameters.Add("ABPERLKZ", OracleType.NVarChar).Value = dwABPOS["ABPERLKZ"].ToString();
                setLocal = Connect2Proxy(or1);
                
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABPOS";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("ABPSOLLMENGE1");
                    Q100.Enqueue("ABPRESMGE");
                    Q100.Enqueue("GWEFTDEN");
                    Q100.Enqueue("SBEAMLENG");
                    Q100.Enqueue("ABPEALFM"); 
                    DivideBy100(tableLocal, Q100);
                    byte[] b;
                    string value;
                    string column="";
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        column="ABPFARBE";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                        column = "GREEDNO";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                        column = "WPRODUCER";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;                        
                    }
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        public DataSet LoadABPOS(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            set4Return.Tables.Add("ABPOS");
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                string ABPAUFKRS = dwABPOS["ABPAUFKRS"].ToString();
                string ABPAUFLFD = dwABPOS["ABPAUFLFD"].ToString();
                string ABPAUFPOS = dwABPOS["ABPAUFPOS"].ToString();
                string ABPART = dwABPOS["ABPART"].ToString();
                //string ABPLIEFNR = dwABPOS["LSNAME1"].ToString().Trim();
              
                or1.CommandText = "SELECT DISTINCT ABPAUFKRS,ABPAUFLFD,ABPWVDAT,ABPAUFPOS,ABPART, ABPVAR, ABPEALFM,ABPFARBE,'#' AS GAART,'#' AS GAVAR,'#' AS GAFARBE"
              + ",ABPBESCHICH,ABPRESMGE,ABPSOLLMENGE1,ABPRESSTK,ABPZUGSTK,ABPLIEFDAT,P.ABPERLKZ,P.ABPERLDAT,LSNAME1 AS WPRODUCER"
                 + ",WARFDDIS AS GWEFTDEN,WAEINZNR AS GREEDNO,'' AS WEFTYARNSTATUS"
                 + ",ABADIAUFKRS AS WBAUFKRS,ABADIAUFLFD AS WBAUFLFD,ABADIAUFPOS AS WBAUFPOS,ABPAUFART"
                 + ",ABFKENNUNG,ABFISTARTDAT,ABFIENDDAT"
                 + " FROM ABPOS P LEFT JOIN WASTAMM ON P.ABPART=WAVWART AND TRIM(WAVWVAR)=TRIM(ABPVAR)"
                 + " LEFT JOIN ABAUFM ON ABAAPOS='0001' AND ABAAUFKRS=ABPAUFKRS AND ABAAUFLFD=ABPAUFLFD AND ABAAUFPOS=ABPAUFPOS "
                 + " LEFT JOIN LIEFDAT L ON  P.ABPLIEFNR=L.LSLIEFNR"
                 + " LEFT JOIN ABFOLGE F ON F.ABFAUFKRS=P.ABPAUFKRS AND F.ABFAUFLFD=P.ABPAUFLFD AND F.ABFAUFPOS=P.ABPAUFPOS AND ABFKENNUNG='RP'"
                 + "  WHERE ABPMANDANT='00'   AND ABPAUFART='PR'"
                 + " AND ABPERLKZ=:ABPERLKZ " 
                 + " AND ABADIAUFKRS LIKE '%" + ABPAUFKRS + "%' AND ABADIAUFLFD LIKE '%" + ABPAUFLFD
                 + "%' AND ABADIAUFPOS LIKE '%" + ABPAUFPOS + "%' AND ABPART LIKE '%" + ABPART + "%' "
                 //+ " AND ABPLIEFNR LIKE '%" + ABPLIEFNR + "%' "
                  + " ORDER BY  1,2,3";

                or1.Parameters.Add("ABPERLKZ", OracleType.NVarChar).Value = dwABPOS["ABPERLKZ"].ToString();
                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABPOS";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("ABPSOLLMENGE1");
                    Q100.Enqueue("ABPRESMGE");
                    Q100.Enqueue("GWEFTDEN");
                    Q100.Enqueue("SBEAMLENG");
                    Q100.Enqueue("ABPEALFM");
                    DivideBy100(tableLocal, Q100);
                    byte[] b;
                    string value;
                    string column = "";
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        column = "ABPFARBE";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                        column = "GREEDNO";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                        column = "WPRODUCER";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                    }
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        public override DataSet GetVKPOS(DataRow dwABPOS)
        {
            DataSet setLocal1 = base.GetVKPOS(dwABPOS);
            string ABAVKAUFTYP = "";
            string LSNAME1 = "";
            string sTemp = "";
            if (setLocal1.Tables.Contains("ABAUFM"))
            {
                foreach (DataRow dwNEW in setLocal1.Tables["ABAUFM"].Rows)
                {
                    ABAVKAUFTYP = dwNEW["ABAVKAUFTYP"].ToString().Trim();
                    if (ABAVKAUFTYP == "K")
                    {
                        dwABPOS["VKPAUFKRS"] = dwNEW["ABAVKAUFKRS"];
                        dwABPOS["VKPAUFLFD"] = dwNEW["ABAVKAUFLFD"];
                        dwABPOS["VKPAUFPOS"] = dwNEW["ABAVKAUFPOS"];
                        dwABPOS["KDKUNDNA"] = dwNEW["KDKUNDNA"];
                        dwABPOS["ABARESQTY"] = dwNEW["ABARESQTY"];
                        dwABPOS["VKPMENGE1"] = dwNEW["VKPMENGE1"];
                        dwABPOS["STDBATCHNO"] = dwNEW["STDBATCHNO"];
                        dwABPOS["VKPWUTERM"] = dwNEW["VKPWUTERM"];
                        dwABPOS["VKPPOSTYP"] = dwNEW["VKPPOSTYP"];
                        
                        Tracer.Verbose(" 留位到客单", dwABPOS["VKPAUFKRS"].ToString() + "." + dwABPOS["VKPAUFLFD"].ToString() + "." + dwABPOS["VKPAUFPOS"].ToString());
                    }
                }
            }
            return setLocal1;
        }
        public DataSet GetFFABRICEINFOByDI(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                #region source code
                /*
                  SELECT DISTINCT P.ABPAUFKRS as SBAUFKRS,P.ABPAUFLFD AS SBAUFLFD,P.ABPAUFPOS AS SBAUFPOS,LSLIEFNA,LSNAME1 AS SPRODUCER
FROM ABPOS P LEFT JOIN ABAUFM M ON P.ABPAUFART ='VW' AND P.ABPAUFKRS=M.ABAAUFKRS AND P.ABPAUFLFD=M.ABAAUFLFD AND P.ABPAUFPOS=M.ABAAUFPOS
LEFT JOIN ABFOLGE F ON  P.ABPAUFKRS=ABFAUFKRS AND P.ABPAUFLFD= ABFAUFLFD AND P.ABPAUFPOS=F.ABFAUFPOS LEFT JOIN LIEFDAT L ON  F.ABFLIEFNR=L.LSLIEFNR WHERE   M.ABAVKAUFKRS='PR' AND M.ABAVKAUFLFD='100428' AND M.ABAVKAUFPOS='010'
                 */
                #endregion
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT P.ABPAUFKRS as FAUFKRS,P.ABPAUFLFD AS FAUFLFD,P.ABPAUFPOS AS FAUFPOS"
                    + ",P.ABPBESCHICH AS FFINISH"
                    + ",LSLIEFNA,LSNAME1 AS FPRODUCER"
                + "  FROM ABPOS P LEFT JOIN ABAUFM M ON P.ABPAUFKRS=M.ABAVKAUFKRS AND P.ABPAUFLFD=M.ABAVKAUFLFD AND P.ABPAUFPOS=M.ABAVKAUFPOS"
                + " LEFT JOIN ABFOLGE F ON  P.ABPAUFKRS=ABFAUFKRS AND P.ABPAUFLFD= ABFAUFLFD AND P.ABPAUFPOS=F.ABFAUFPOS"
                + " LEFT JOIN LIEFDAT L ON  F.ABFLIEFNR=L.LSLIEFNR"
                + " WHERE P.ABPAUFART ='VA' AND  M.ABAAUFKRS=:ABPAUFKRS AND M.ABAAUFLFD=:ABPAUFLFD AND M.ABAAUFPOS=:ABPAUFPOS";
                or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("ABPAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABFOLGE";

                    byte[] b;
                    string value;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["FPRODUCER"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["FPRODUCER"] = value;
                        dwABPOS["FPRODUCER"] = value;
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["FFINISH"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["FFINISH"] = value;
                        dwABPOS["FFINISH"] = value;
                        Tracer.Verbose(" 后整理：", dwABPOS["FFINISH"].ToString() + " 在工厂" + dwABPOS["FPRODUCER"].ToString());
                    }
                }
                set4Return.Merge(setLocal);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        private DataSet GetSBATCHNOByDI(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                #region source code
                /*
                  SELECT DISTINCT P.ABPAUFKRS as SBAUFKRS,P.ABPAUFLFD AS SBAUFLFD,P.ABPAUFPOS AS SBAUFPOS,LSLIEFNA,LSNAME1 AS SPRODUCER
FROM ABPOS P LEFT JOIN ABAUFM M ON P.ABPAUFART ='VW' AND P.ABPAUFKRS=M.ABAAUFKRS AND P.ABPAUFLFD=M.ABAAUFLFD AND P.ABPAUFPOS=M.ABAAUFPOS
LEFT JOIN ABFOLGE F ON  P.ABPAUFKRS=ABFAUFKRS AND P.ABPAUFLFD= ABFAUFLFD AND P.ABPAUFPOS=F.ABFAUFPOS LEFT JOIN LIEFDAT L ON  F.ABFLIEFNR=L.LSLIEFNR WHERE   M.ABAVKAUFKRS='PR' AND M.ABAVKAUFLFD='100428' AND M.ABAVKAUFPOS='010'
                 */
                #endregion
                string WBAUFKRS = dwABPOS["WBAUFKRS"].ToString();
                string WBAUFLFD = dwABPOS["WBAUFLFD"].ToString();
                string WBAUFPOS = dwABPOS["WBAUFPOS"].ToString();
                if (WBAUFLFD == "000000")
                {
                    return set4Return;
                }
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT P.ABPAUFKRS as SBAUFKRS,P.ABPAUFLFD AS SBAUFLFD,P.ABPAUFPOS AS SBAUFPOS"
                    + ",LSLIEFNA,LSNAME1 AS SPRODUCER"
                + "  FROM ABPOS P LEFT JOIN ABAUFM M ON  P.ABPAUFKRS=M.ABAAUFKRS AND P.ABPAUFLFD=M.ABAAUFLFD AND P.ABPAUFPOS=M.ABAAUFPOS"
                + " LEFT JOIN ABFOLGE F ON  P.ABPAUFKRS=ABFAUFKRS AND P.ABPAUFLFD= ABFAUFLFD AND P.ABPAUFPOS=F.ABFAUFPOS"
                + " LEFT JOIN LIEFDAT L ON  F.ABFLIEFNR=L.LSLIEFNR"
                    + " WHERE P.ABPAUFART ='VW' AND"
                + "  M.ABAVKAUFKRS=:ABPAUFKRS AND M.ABAVKAUFLFD=:ABPAUFLFD AND M.ABAVKAUFPOS=:ABPAUFPOS";
                or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = dwABPOS["WBAUFKRS"].ToString();
                or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = dwABPOS["WBAUFLFD"].ToString();
                or1.Parameters.Add("ABPAUFPOS", OracleType.NVarChar).Value = dwABPOS["WBAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);
                string sTemp = "";
                string SIZEBATCHNO = "", SPRODUCER = "";
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABFOLGE";

                    byte[] b;
                    string value;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["SPRODUCER"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["SPRODUCER"] = value;
                        sTemp = dwTemp["SBAUFKRS"].ToString().Trim() + dwTemp["SBAUFLFD"].ToString().Trim() + dwTemp["SBAUFPOS"].ToString().Trim() + ";";
                        if (!SIZEBATCHNO.Contains(sTemp)
                             && sTemp != ";")
                        {
                            SIZEBATCHNO += sTemp;
                        }
                        sTemp = dwTemp["SPRODUCER"].ToString().Trim() + ";";
                        if (!SPRODUCER.Contains(sTemp)
                            && sTemp != ";")
                        {
                            SPRODUCER += sTemp;
                        }
                    }

                    SIZEBATCHNO += "***";
                    SIZEBATCHNO = SIZEBATCHNO.Replace(";***", "");
                    SIZEBATCHNO = SIZEBATCHNO.Replace("***", "");
                    SIZEBATCHNO = SIZEBATCHNO.Replace(";", Environment.NewLine);
                    dwABPOS["SIZEBATCHNO"] = SIZEBATCHNO;
                    SPRODUCER += "***";
                    SPRODUCER = SPRODUCER.Replace(";***", "");
                    SPRODUCER = SPRODUCER.Replace("***", "");
                    SPRODUCER = SPRODUCER.Replace(";", Environment.NewLine);
                    dwABPOS["SPRODUCER"] = SPRODUCER;
                    Tracer.Verbose(" 浆纱厂", SPRODUCER);
                }
                set4Return.Merge(setLocal);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        public override DataSet GetReservedOrder(DataRow dwABPOS, ref ArrayList listDIOrder, ref ArrayList listPROrder,string OrderNum="")
        {
            AB45AV95Data55 m_Data45 = new AB45AV95Data55();
            AB50AV95Data55 m_Data50 = new AB50AV95Data55();
            m_Data45.proxy = this.proxy;
            m_Data45.OracleDB = this.OracleDB;
            DataSet set4Return = new DataSet();
            DataSet setResult = new DataSet();
            DataTable tableLocal = null;
            DataRow dwNABPOS = dwABPOS.Table.Clone().NewRow();
            try
            {
                dwNABPOS["ABPAUFKRS"] = dwABPOS["WBAUFKRS"].ToString();
                dwNABPOS["ABPAUFLFD"] = dwABPOS["WBAUFLFD"].ToString();
                dwNABPOS["ABPAUFPOS"] = dwABPOS["WBAUFPOS"].ToString();

                setResult = base.GetReservedOrder(dwNABPOS,ref listDIOrder,ref listPROrder,OrderNum);
                if (setResult.Tables.Count > 0)
                { 
                    byte[] b;
                    string value;
                    string OrderType = "";
                    string sTemp = "";
                    string SIZEBATCHNO = "", SPRODUCER="";
                    string VWORDERNUM = "";
                    double BeamLength = 0;
                    int SBEAMNO = 0;
                 
           
                    foreach (DataRow dwTemp in setResult.Tables["RESERVEDORDER"].Rows)
                    {
                        OrderType = dwTemp["ABPAUFART"].ToString().Trim();
                        #region VW
                        if (OrderType == "VW")
                        {
                            dwNABPOS["ABPAUFKRS"] = dwTemp["ABPAUFKRS"].ToString();
                            dwNABPOS["ABPAUFLFD"] = dwTemp["ABPAUFLFD"].ToString();
                            dwNABPOS["ABPAUFPOS"] = dwTemp["ABPAUFPOS"].ToString();
                            m_Data45.GetBABST(dwNABPOS, dwABPOS);
                             m_Data45.GetMATYARNINFO(dwNABPOS,dwABPOS); 
                            BeamLength += System.Convert.ToDouble(dwTemp["ABPSOLLMENGE1"].ToString());
                            SBEAMNO += System.Convert.ToInt16(dwTemp["ABPSOLLMENGE8"].ToString());
                            sTemp = dwTemp["ABPAUFKRS"].ToString()
                                                                                   + "-" + dwTemp["ABPAUFLFD"].ToString()
                                                                                   + "-" + dwTemp["ABPAUFPOS"].ToString();
                            dwABPOS["JSPLANDATE"] = dwTemp["ABPWVDAT"];
                            dwABPOS["VWPSTARTDATE"] = dwTemp["ABPERLDAT"];
                            dwABPOS["VWPENDDATE"] = dwTemp["ABPLIEFDAT"];
                            dwABPOS["VWAENDDATE"] = dwTemp["ABPERLDAT"];
                            dwABPOS["SBEAMDATE"] = dwTemp["ABPERLDAT"]; 
                            dwABPOS["VWFARBE"] = dwTemp["BSFBEZ"];
                            if (!VWORDERNUM.Contains(sTemp + ";"))
                            {
                                VWORDERNUM += sTemp + ";";
                                listPROrder.Add(sTemp);
                            }

                            sTemp = dwTemp["EXTPRODUCER"].ToString();
                            if (!SPRODUCER.Contains(sTemp + ";"))
                            {
                                SPRODUCER += sTemp + ";";
                            }
                        }
                        #endregion

                        #region GP
                        if (OrderType == "GP")
                        {
                            dwABPOS["YDPENDDATE"] = dwTemp["ABPLIEFDAT"];
                            dwABPOS["YDAENDDATE"] = dwTemp["ABPERLDAT"];
                        }
                        #endregion
                    }
                    #region Finalize
                    VWORDERNUM += "***";
                    VWORDERNUM = VWORDERNUM.Replace(";***", "");
                    VWORDERNUM = VWORDERNUM.Replace("***", "");
                    dwABPOS["VWORDERNUM"] = VWORDERNUM;
                    SPRODUCER += "***";
                    SPRODUCER = SPRODUCER.Replace(";***", "");
                    SPRODUCER = SPRODUCER.Replace("***", "");
                    dwABPOS["SPRODUCER"] = SPRODUCER;
                    dwABPOS["SBEAMLENG"] = BeamLength;
                    dwABPOS["SBEAMNO"] = SBEAMNO; 
                    #endregion
                }

                set4Return.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        private DataSet GetSBATCHNO(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                #region source code
                /*
                   SELECT BBWERTDAT,BMBEM FROM BABEW W LEFT JOIN BABST ON BMBAUMKRS=BBBAUMKRS AND BMBAUMLFD=BBBAUMLFD
  WHERE BBBAUMKRS='SZ' AND BBBAUMLFD='000337' AND BBBS IN ('13','21')
              */
                #endregion
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = " SELECT BBWERTDAT,BMBEM,BBBS FROM BABEW W "
                + " LEFT JOIN BABST ON BMBAUMKRS=BBBAUMKRS AND BMBAUMLFD=BBBAUMLFD "
                + "  WHERE BBBS IN ('13','21') "
                + " AND BBBAUMKRS||BBBAUMLFD=:SBEAMNO";
                or1.Parameters.Add("SBEAMNO", OracleType.NVarChar).Value = dwABPOS["SBEAMNO"].ToString();
                setLocal = Connect2Proxy(or1);
                string BBBS = "", sTemp = "", SIZEBATCHNO = "", GPRODUCER = "";
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "BABEW";
                    byte[] b;
                    string value;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["BMBEM"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["BMBEM"] = value;
                        BBBS = dwTemp["BBBS"].ToString().Trim();
                        if (BBBS == "13")
                        {
                            dwABPOS["SENDDATE"] = dwTemp["BBWERTDAT"];
                            Tracer.Verbose(" 浆纱完毕时间", dwABPOS["SENDDATE"].ToString());
                        }
                        /* if (BBBS == "21")
                         {
                             dwABPOS["GSTARTDATE"] = dwTemp["BBWERTDAT"];
                             Tracer.Verbose(" 上机时间", dwABPOS["GSTARTDATE"].ToString());
                         }*/
                        sTemp = dwTemp["BMBEM"].ToString().Trim() + ";";
                        if (!SIZEBATCHNO.Contains(sTemp) && sTemp != ";")
                        {
                            SIZEBATCHNO += sTemp;
                        }
                    }
                    SIZEBATCHNO += "***";
                    SIZEBATCHNO = SIZEBATCHNO.Replace(";***", "");
                    SIZEBATCHNO = SIZEBATCHNO.Replace("***", "");
                    SIZEBATCHNO = SIZEBATCHNO.Replace(";", Environment.NewLine);
                    dwABPOS["SIZEBATCHNO"] = SIZEBATCHNO;
                    Tracer.Verbose(" 浆纱缸号", SIZEBATCHNO);
                }
                set4Return.Merge(setLocal);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        public DataSet GetMATYARNINFO(DataRow dwUIABPOS,DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            set4Return.Tables.Add("GAAUF");
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                /*
                  SELECT GAART,GAVAR,GAFARBE,GAKG1,GAKG2,GAKG3,GAKG4,GAKG5,GAKG6,GAKG7 FROM ABPOS P LEFT JOIN GAAUF F ON P.ABPAUFKRS=F.GAAUFKRS AND P.ABPAUFLFD=F.GAAUFLFD AND P.ABPAUFPOS=F.GAAUFPOS
WHERE GAAUFKRS='PR' AND  GAAUFLFD='100428' AND GAAUFPOS='010'
                 */
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT ZUNEUDAT, GAART,GAVAR,GAFARBE,GAKG1,GAKG2,GAKG3,GAKG4,GAKG5,GAKG6,GAKG7,'' AS WEFTYARNSTATUS"
                  + " FROM ABPOS P LEFT JOIN GAAUF F ON P.ABPAUFKRS=F.GAAUFKRS AND P.ABPAUFLFD=F.GAAUFLFD AND P.ABPAUFPOS=F.GAAUFPOS"
                  + " LEFT JOIN ZUTEI Z ON ZUAUFKRS=GAAUFKRS AND ZUAUFLFD=GAAUFLFD AND ZUAUFPOS=GAAUFPOS "
                  + " WHERE GAAUFKRS=:ABPAUFKRS AND  GAAUFLFD=:ABPAUFLFD AND GAAUFPOS=:ABPAUFPOS";

                or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = dwUIABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = dwUIABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("ABPAUFPOS", OracleType.NVarChar).Value = dwUIABPOS["ABPAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);
                int weftcount = 0;
                string sTemp = "", GWEFTINFO = "", WEFTYARNSTATUS = "";
                decimal GAKG1 = 0, GAKG3 = 0, GAKG5 = 0, GAKG6 = 0;
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "GAAUF";
                    SetDefault(tableLocal);
                    Queue Q100 = new Queue();
                    Q100.Enqueue("GAKG1");
                    Q100.Enqueue("GAKG3");
                    Q100.Enqueue("GAKG5");
                    Q100.Enqueue("GAKG6");
                    DivideBy100(tableLocal, Q100);
                    byte[] b;
                    string value;
                    string GAART,GAVAR,GAFARBE;
                    double ZUNEUDAT = 0, WFAENDDATE = 0;
                    WFAENDDATE = System.Convert.ToDouble(dwABPOS["WFAENDDATE"].ToString());
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        #region WFAENDDATE
                        ZUNEUDAT = System.Convert.ToDouble(dwTemp["ZUNEUDAT"].ToString());
                        if (WFAENDDATE < ZUNEUDAT)
                        {
                            WFAENDDATE = ZUNEUDAT;
                        }
                        #endregion
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["GAFARBE"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["GAFARBE"] = value;
                        weftcount++;
                        sTemp = dwTemp["GAKG1"].ToString().Trim();
                        GAKG1 = System.Convert.ToDecimal(sTemp);
                        sTemp = dwTemp["GAKG3"].ToString().Trim();
                        GAKG3 = System.Convert.ToDecimal(sTemp);
                        sTemp = dwTemp["GAKG5"].ToString().Trim();
                        GAKG5 = System.Convert.ToDecimal(sTemp);
                        sTemp = dwTemp["GAKG6"].ToString().Trim();
                        GAKG6 = System.Convert.ToDecimal(sTemp);
                        if (GAKG1 > GAKG5)
                        {
                            sTemp = "";
                            sTemp += dwTemp["GAART"].ToString().Trim() + ".";
                            sTemp += dwTemp["GAVAR"].ToString().Trim() + ".";
                            sTemp += dwTemp["GAFARBE"].ToString().Trim();
                            sTemp += "总需求" + GAKG1.ToString() + "kg,";
                            sTemp += "已留位" + GAKG3.ToString() + "kg,";
                            sTemp += "已发纱" + GAKG5.ToString() + "kg,";
                            sTemp += "已消耗" + GAKG6.ToString() + "kg.";
                            if (GAKG5 - GAKG6 > 0)
                            { 
                                GAART=dwTemp["GAART"].ToString().Trim();
                                GAVAR=dwTemp["GAVAR"].ToString().Trim();
                                GAFARBE=dwTemp["GAFARBE"].ToString().Trim();
                                WEFTYARNSTATUS+=GAART+"."+GAVAR+"."+GAFARBE+"."+"剩余纱线："+(GAKG5-GAKG6).ToString()+";";
                            }
                            if (GAKG1 - GAKG3 - GAKG5 > 0)
                            {
                                GWEFTINFO += sTemp + "待料@" + (GAKG1 - GAKG3 - GAKG5).ToString() + "kg;";
                            }
                            else
                            {
                                GWEFTINFO += sTemp + ";";
                            }
                        }
                    }
                    GWEFTINFO += "***";
                    GWEFTINFO = GWEFTINFO.Replace(";***", "");
                    GWEFTINFO = GWEFTINFO.Replace("***", "");
               
                    //GWEFTINFO = GWEFTINFO.Replace(";", Environment.NewLine);
                    if (GWEFTINFO.Contains("待料"))
                    {
                        GWEFTINFO = "共" + weftcount.ToString() + "色纬纱，其中" + GWEFTINFO;
                    }
                    else
                    {
                        GWEFTINFO = "共" + weftcount.ToString() + "色纬纱都已到位。";
                    }
                    dwABPOS["GWEFTINFO"] = GWEFTINFO;
                    Tracer.Verbose(" 纬纱信息", GWEFTINFO);
                    Tracer.Speak(" 纬纱信息" + GWEFTINFO); 
                    WEFTYARNSTATUS += "***";
                    WEFTYARNSTATUS = WEFTYARNSTATUS.Replace(";***", "");
                    WEFTYARNSTATUS = WEFTYARNSTATUS.Replace("***", "");
                    WEFTYARNSTATUS = WEFTYARNSTATUS.Replace(";", Environment.NewLine);
                    dwABPOS["WEFTYARNSTATUS"] = WEFTYARNSTATUS;
                    Tracer.Speak(WEFTYARNSTATUS); 
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        public DataSet GetABFOLGE(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                #region code reference
                /*
                  SELECT DISTINCT LSLIEFNA,LSNAME1,ABFMNRKRS||ABFMLFDNR||ABFMPOS AS GLOOMNUM FROM ABFOLGE F,LIEFDAT L  WHERE ABFMANDANT='00'
AND   F.ABFAUFKRS='PR' AND  ABFAUFLFD='100428' AND  ABFAUFPOS='010'  AND F.ABFLIEFNR=L.LSLIEFNR
                 */
                #endregion
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = " SELECT DISTINCT LSLIEFNA,LSNAME1,ABFMNRKRS||ABFMLFDNR||ABFMPOS AS GLOOMNUM,ABFISTARTDAT"
                + " FROM ABFOLGE F,LIEFDAT L "
                + " WHERE ABFMANDANT='00'  AND  ABFAUFKRS=:ABFAUFKRS AND ABFAUFLFD=:ABFAUFLFD AND ABFAUFPOS=:ABFAUFPOS "
                + " AND F.ABFLIEFNR=L.LSLIEFNR";

                or1.Parameters.Add("ABFAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("ABFAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("ABFAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);
                string sTemp = "";
                string GPRODUCER = "", GLOOMNUM = "";
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABFOLGE";

                    byte[] b;
                    string value;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["LSNAME1"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["LSNAME1"] = value;
                        sTemp = dwTemp["LSNAME1"].ToString().Trim() + ";";
                        if (!GPRODUCER.Contains(sTemp)
                             && sTemp != ";")
                        {
                            GPRODUCER += sTemp;
                        }
                        sTemp = dwTemp["GLOOMNUM"].ToString().Trim() + ";";
                        if (!GLOOMNUM.Contains(sTemp) && sTemp != "000000;")
                        {
                            GLOOMNUM += sTemp;
                        }
                        dwABPOS["GSTARTDATE"] = dwTemp["ABFISTARTDAT"].ToString().Trim();
                    }
                    GPRODUCER += "***";
                    GPRODUCER = GPRODUCER.Replace(";***", "");
                    GPRODUCER = GPRODUCER.Replace("***", "");
                    GPRODUCER = GPRODUCER.Replace(";", Environment.NewLine);
                    dwABPOS["GPRODUCER"] = GPRODUCER;
                    GLOOMNUM += "***";
                    GLOOMNUM = GLOOMNUM.Replace(";***", "");
                    GLOOMNUM = GLOOMNUM.Replace("***", "");
                    GLOOMNUM = GLOOMNUM.Replace(";", Environment.NewLine);
                    dwABPOS["GLOOMNUM"] = GLOOMNUM;
                    Tracer.Verbose(" 织造厂", GPRODUCER);
                }
                set4Return.Merge(setLocal);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        #endregion
        public override void BuildABPOS(DataRow dwABPOS)
        {
            DataRow dwNABPOS = dwABPOS.Table.Clone().NewRow();
            ArrayList listDIOrder = new ArrayList(), listPROrder = new ArrayList();
            dwNABPOS.ItemArray = dwABPOS.ItemArray;
            DataSet set4Return = new DataSet();
            ABPOS = dwNABPOS["ABPAUFKRS"].ToString() + "." + dwNABPOS["ABPAUFLFD"].ToString() + "." + dwNABPOS["ABPAUFPOS"].ToString();
            ABPOS = ThreadCount.ToString() + ": " + ABPOS;
            try
            {
                string ABAVKAUFTYP = "";
                string LSNAME1 = "";
                string sTemp = "";
                #region----------------------- 查询客单----------------------------
                GetVKPOS(dwNABPOS);
                #endregion
                #region----------------------- 查询后整理----------------------------
                GetFFABRICEINFOByDI(dwNABPOS);
                #endregion
                #region----------------------- 查询浆纱厂----------------------------
                GetReservedOrder(dwNABPOS,ref listDIOrder,ref listPROrder);
                #endregion
                #region----------------------- 织造厂----------------------------
                GetABFOLGE(dwNABPOS);
                #endregion
                #region-----------------------留位轴信息----------------------------
                GetSBATCHNO(dwNABPOS);
                #endregion
                #region-----------------------纬纱信息----------------------------
                GetMATYARNINFO(dwNABPOS, dwNABPOS);
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            finally
            {
                try
                {
                    dwABPOS.ItemArray = dwNABPOS.ItemArray;
                    string progress = "查询进展:[" + ThreadID.ToString() + "] 完成<-FinishedJobs->/<-TotalJobs->";
                    EventDispatcher.Instance.RaiseStatus(progress, StatusEventType.StartProgress);
                }
                catch (Exception ex)
                {
                    Tracer.Error(ABPOS + ex.StackTrace, ex);
                }
            }
        }
    }
}